﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class frmCarTypeSet : Form
    {
        public frmCarTypeSet()
        {
            InitializeComponent();
        }

        private void frmCarTypeSet_Load(object sender, EventArgs e)
        {
            Bind();

        }
        private void Bind()
        {
            this.txtName.Text = "";
            this.lstCarType.Items.Clear();
            CarType carType = new CarType();
            DataTable dt = carType.Query();
            foreach (DataRow row in dt.Rows)
            {
                this.lstCarType.Items.Add(row["类型名称"].ToString());
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CarType carType = new CarType();
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                MessageHelper.ShowInfo("请输入类型名称！");
                return;
            }
            if (carType.Query(" CarTypeName='" + this.txtName.Text.Trim() + "'").Rows.Count > 0)
            {
                MessageHelper.ShowInfo("该类型名称已经存在！");
                return;
            }
            carType.ID = Guid.NewGuid().ToString();
            carType.CarTypeName = this.txtName.Text.Trim();
            carType.Insert();
            Bind();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.lstCarType.SelectedItem != null)
            {
                CarType carType = new CarType();
                carType.DeleteWithCondition(" CarTypeName='" + this.lstCarType.SelectedItem.ToString() + "'");
                Bind();
            }
            else
            {
                MessageHelper.ShowInfo("请选择要删除的记录！");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
